import axios from 'axios';

/**
 * get 请求
 * @param {string} url      请求地址
 * @param {object} params    query参数
 * @param {object} data      body参数
 * @param {string} method     请求方式
 * @returns {Promise <any>}
 */
function getRequest(url, data, params, method = 'GET') {
  return new Promise((resolve, reject) => {
    axios({
      url: url,
      method: method,
      data: data,
      params: params,
    }).then((res) => {
      resolve(res.data);
    }).catch((err) => {
      reject(err);
    })
  })
}

/**
 * 
 * @param {string} url      请求地址
 * @param {object} params    query参数
 * @param {object} data      body参数
 * @param {string} method     请求方式
 * @returns {Promise <any>}
 */
// post 请求
function postRequest(url, data, params, method = 'POST') {
  return new Promise((resolve, reject) => {
    axios({
      url: url,
      method: method,
      data: data,
      params: params,
    }).then((res) => {
      resolve(res.data);
    }).catch((err) => {
      reject(err);
    })
  })
}

/**
 * 带 header 的 get
 * @param {string} url 
 * @param {object} data 
 * @param {object} params 
 * @param {string} method 
 * @param {string} header
 * @returns {Promise <any>}
 */
function getRequests(url, data, params, header, method = 'GET') {
  return new Promise((resolve, reject) => {
    axios({
      url: url,
      method: method,
      data: data,
      params: params,
      headers: {
        'Authorization': header.token
      }
    }).then((res) => {
      resolve(res.data);
    }).catch((err) => {
      reject(err);
    })
  })
}

/**
 * 带 header 的post
 * @param {string} url      请求地址
 * @param {object} params    query参数
 * @param {object} data      body参数
 * @param {string} method     请求方式
 * @param {object} header     header
 * @returns {Promise <any>}
 */
// post 请求
function postRequests(url, data, params, header, method = 'POST') {
  return new Promise((resolve, reject) => {
    axios({
      url: url,
      method: method,
      data: data,
      params: params,
      headers: {
        'Authorization': header.token
      }
    }).then((res) => {
      resolve(res.data);
    }).catch((err) => {
      reject(err);
    })
  })
}
export default {
  get: getRequest,
  post: postRequest,
  gets: getRequests,
  posts: postRequests
}